package com.logic.landseaserver.ws.interceptor;

import com.logic.common.enums.LogTypeEnum;
import com.logic.common.enums.ModuleTypeEnum;
import com.logic.common.security.SecurityUtils;
import com.logic.common.util.SysUtil;
import com.logic.landseaserver.common.enums.LogModuleTypeEnum;
import com.logic.landseaserver.domain.Property;
import com.logic.landseaserver.domain.PropertyCheck;
import com.logic.landseaserver.domain.PropertyCheckEstimation;
import com.logic.landseaserver.service.Action;
import com.logic.landseaserver.service.IOperationLogService;
import com.logic.landseaserver.service.impl.ProjectServiceImpl;
import com.logic.landseaserver.ws.ContractRest;
import com.logic.landseaserver.ws.request.*;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * Created by quintina on 2017/8/10.
 */
@Aspect
@Component
public class PropertyLogAspect {
    private static final Logger logger = LoggerFactory.getLogger(ContractRest.class);

    @Autowired
    private IOperationLogService logService;


    @AfterReturning("execution(* com.logic.landseaserver.service.impl.PropertyServiceImpl.*(..)) && @annotation(com.logic.landseaserver.service.Action)" +
            "|| execution(* com.logic.landseaserver.service.impl.PropertyCheckServiceImpl.*(..)) && @annotation(com.logic.landseaserver.service.Action)"+
            "|| execution(* com.logic.landseaserver.service.impl.PropertyDecisionServiceImpl.*(..)) && @annotation(com.logic.landseaserver.service.Action)"+
            "|| execution(* com.logic.landseaserver.service.impl.PropertySignServiceImpl.*(..)) && @annotation(com.logic.landseaserver.service.Action)")
    public void createLog(JoinPoint joinPoint) throws ClassNotFoundException {
        Object[] arguments = joinPoint.getArgs();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Class targetClass = Class.forName(targetName);
        Method[] methods = targetClass.getMethods();
        String ationName = null;
        Integer propertyId = null;
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                Class[] clazzs = method.getParameterTypes();
                if (clazzs.length == arguments.length && method.getAnnotation(Action.class) != null) {
                    ationName = method.getAnnotation(Action.class).name();
                }
                if (method.getName() == "insert") {
                }
            }
        }

        if (ationName != null && ationName.length() > 0) {
            Class argClass = arguments[0].getClass();
            if (argClass == Integer.class) {
                propertyId = (Integer) arguments[0];
            }
            if (argClass == PropertyReq.class) {
                PropertyReq property = (PropertyReq) arguments[0];
                propertyId = property.getId();
            }
            if(argClass == PropertyCheck.class){
                PropertyCheck req = (PropertyCheck) arguments[0];
                propertyId = req.getPropertyId();
            }
            if(argClass == PropertySignEstimationReq.class){
                PropertySignEstimationReq req = (PropertySignEstimationReq) arguments[0];
            }
            if(argClass == PropertySignReq.class){
                PropertySignReq req = (PropertySignReq) arguments[0];
                propertyId = req.getPropertyId();
            }
            if (argClass == SuspendPropertyReq.class) {
                SuspendPropertyReq req = (SuspendPropertyReq) arguments[0];
                propertyId = req.getId();
            }
            if (argClass == PropertyCheckEstimation.class) {
                PropertyCheckEstimation req = (PropertyCheckEstimation) arguments[0];
                propertyId = req.getPropertyId();
            }
            if(argClass == PropertyCheckDeleteReq.class){
                PropertyCheckDeleteReq req = (PropertyCheckDeleteReq) arguments[0];
                propertyId = req.getPropertyId();
            }

            if(argClass == PropertyDecisionReq.class){
                PropertyDecisionReq req = (PropertyDecisionReq) arguments[0];
                propertyId = req.getPropertyId();
            }
            if (argClass == PropertyStatusHistoryReq.class) {
                PropertyStatusHistoryReq req = (PropertyStatusHistoryReq) arguments[0];
                propertyId = req.getId();
            }
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

            logService.log(SysUtil.getClientIpAddr(request),
                    SecurityUtils.getCurrentLogin().getUserId(), ationName,
                    LogTypeEnum.EDIT.name(), LogModuleTypeEnum.PROPERTY.getCode(), propertyId);
        }

    }

    @AfterReturning(returning = "rvt", pointcut = "execution(* com.logic.landseaserver.service.impl.PropertyServiceImpl.insert(..))")
    public void AfterReturning(Object rvt)  {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        logService.log(SysUtil.getClientIpAddr(request),
                SecurityUtils.getCurrentLogin().getUserId(), "创建",
                LogTypeEnum.ADD.name(), LogModuleTypeEnum.PROPERTY.getCode(), (Integer) rvt);
    }

}
